package com.webserver.core;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;

import java.io.File;
import java.lang.reflect.Type;

/**
 * 用于处理请求
 */
public class DispatcherServlet {
    public void service(HttpRequest request, HttpResponse response){
        String path=request.getUri();
        File file= new File("./webapps"+path);


        if (file.exists()&&file.isFile()){
            //正确情况
            response.setEntity(file);
            //设置响应头
            response.putHeader("Content-Type","text/html");
            response.putHeader("Content-Length",file.length()+"");
        }else {
            response.setStatusCode(404);
            response.setStatusReason("NotFound");
            file=new File("./webapps/root/404.html");
            response.setEntity(file);
            response.putHeader("Content-Type","text/html");
            response.putHeader("Content-Length",file.length()+"");

        }
        //该响应头是告知浏览器服务端是谁
        response.putHeader("Server","WebServer");
    }
}
